package com.eatme.util;

import org.springframework.beans.factory.InitializingBean;
import com.eatme.dao.BasicListDao;
import com.eatme.model.BasicList;


public class DataGenerator implements InitializingBean {
    private static final String[] FIRSTFIELDS = {"Jacob", "Emily", "Michael", "Sarah",
            "Matthew", "Brianna", "Nicholas", "Samantha", "Christopher",
            "Hailey", "Abner", "Abby", "Joshua", "Douglas", "Jack",
            "Keith", "Gerald", "Samuel", "Willie", "Larry", "Jose",
            "Timothy", "Sandra", "Kathleen", "Pamela", "Virginia", "Debra",
            "Maria", "Linda"};
    private static final String[] SECONDFIELDS = {"Smiith", "Johnson", "Williams", "Jones",
            "Brown", "Donahue", "Bailey", "Rose", "Allen", "Black",
            "Davis", "Clark", "Hall", "Lee", "Baker", "Gonzalez", "Nelson",
            "Moore", "Wilson", "Graham", "Fisher", "Cruz", "Ortiz",
            "Gomez", "Murray"};
    private BasicListDao dao;
    private int count = 30;

    public void setContactDao(BasicListDao dao) {
        this.dao = dao;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void afterPropertiesSet() throws Exception {
        for (int i = 0; i < count; i++) {
            BasicList contact = new BasicList();
            contact.setFirstField(randomString(FIRSTFIELDS));
            contact.setSecondField(randomString(SECONDFIELDS));

            dao.save(contact);
        }
    }

    private String randomString(String[] choices) {
        return choices[rint(0, choices.length)];
    }

    private int rint(int min, int max) {
        return (int) (Math.random() * (max - min) + min);
    }
}
